package com.hnyr.kdcxzs.application

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.ProcessLifecycleOwner
import com.bytedance.sdk.openadsdk.TTAdSdk

import com.cszy.yydqbfq.common.AppLifecycleTracker
import com.cszy.yydqbfq.listener.GlobaiPlayListener
import com.cszy.yydqbfq.utils.AppUtil
import com.hlyj.http.base.tool.lib_hlyj_base.listener.IChannelProvider

import com.hlyj.http.base.tool.lib_hlyj_base.BaseApplication
import com.hlyj.http.base.tool.lib_hlyj_base.Constants
import com.hlyj.http.base.tool.lib_hlyj_base.utils.BaseModuleSpUtils
import com.hlyj.http.base.tool.lib_hlyj_base.utils.SafeDialogUtils
import com.hnyyac.ad.mds.GMAds
import com.kongzue.dialogx.DialogX
import com.lzx.starrysky.GlobalPlaybackStageListener
import com.lzx.starrysky.StarrySkyInstall
import com.lzx.starrysky.manager.PlaybackStage
import com.tencent.mmkv.MMKV
import org.litepal.LitePal
import java.util.concurrent.CopyOnWriteArrayList

class MyApplication: BaseApplication(),Application.ActivityLifecycleCallbacks,GlobalPlaybackStageListener,
    IChannelProvider {
    // List to hold all registered listeners
    private val playbackListeners = CopyOnWriteArrayList<GlobaiPlayListener>()

    // Method to register listeners
    fun registerPlaybackListener(listener: GlobaiPlayListener) {
        if (!playbackListeners.contains(listener)) {
            playbackListeners.add(listener)
        }
    }

    // Method to unregister listeners
    fun unregisterPlaybackListener(listener: GlobaiPlayListener) {
        playbackListeners.remove(listener)
    }

    override fun onCreate() {
        super.onCreate()
        LitePal.initialize(this)
        registerActivityLifecycleCallbacks(this)
        restartSplashListener()//热启动
        DialogX.init(this)
        MMKV.initialize(this)
        var session = BaseModuleSpUtils.getInstance().getInt(Constants.SESSIONID,0);
        session+=1;
        BaseModuleSpUtils.getInstance().putInt(Constants.SESSIONID,session);
        StarrySkyInstall.init(this)
            .isStartService(false)
            .setGlobalPlaybackStageListener(this)
            .apply()
    }


    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {

    }

    @SuppressLint("SuspiciousIndentation")
    override fun onActivityStarted(activity: Activity) {

    }

    override fun onActivityResumed(activity: Activity) {
        val rootView = activity.window.decorView.rootView
        rootView.viewTreeObserver.addOnWindowFocusChangeListener { hasFocus ->
            if (hasFocus && !activity.isFinishing && !activity.isDestroyed) {
                // 如果 Activity 不属于目标包 且 不是 HtmlActivity，才显示弹窗
                    var privacyId= BaseModuleSpUtils.getInstance().getString(Constants.PRIVACYID)
                    //  Log.e("onActivityResumed", "onActivityResumed: $privacyId")
                    //OverlayViewUtils.removeCustomOverlay()
                    //OverlayViewUtils.showCustomOverlay(activity, privacyId)
                    SafeDialogUtils.showSafeDialog(activity, privacyId)
            }
        }
    }

    override fun onActivityPaused(activity: Activity) {

    }

    override fun onActivityStopped(activity: Activity) {

    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

    }

    override fun onActivityDestroyed(activity: Activity) {
        //OverlayViewUtils.removeCustomOverlay()
        SafeDialogUtils.removeSafeDialog()
    }

    override fun onPlaybackStageChange(stage: PlaybackStage) {
        if (stage.stage == PlaybackStage.PLAYING) {
            // Notify all registered listeners
            for (listener in playbackListeners) {
                listener.onPlayback(stage)
            }

        } else {
            Log.e("PlaybackState", "当前状态: ${stage.stage}")
        }
    }

    private fun  restartSplashListener() {
        // 注册生命周期监听
        ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifecycleTracker)
    }

    override fun getChannelId(): String {
            return AppUtil.getChannel()
    }
}